package ms.core.gateway.request;

import java.util.List;
import java.util.Map;

import lombok.Data;
import ms.core.common.AppException;
import ms.core.common.consts.SysErr;
import ms.core.gateway.bean.SysCorpBean;
import ms.core.gateway.response.DicConstVO.ConstItem;
import ms.core.gateway.utils.DicConstUtils;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;

@Data
public class CreateCorpRequest {
	private String corpName;			// 企业名称
	private String creditCode;			// 企业信用代码,个人用户则为身份证号码
	private int ctype;					// 实体类型,详见 SysCorpBean.ctype
	private String linkMan;				// 联系人
	private String linkTel;				// 联系电话
	private Map<String, Object> ext;	// 实体扩展资料

	public static CreateCorpRequest getInstance(Map<String, Object> body) {
		CreateCorpRequest corp = JsonTool.objectToBean(body, CreateCorpRequest.class);
		if (corp==null) throw new AppException(SysErr.SYS_PARAM_ERR);
		
		if (StrTool.isBlank(corp.getCorpName()))
			throw new AppException(SysErr.SYS_PARAM_ERR, "请输入企业名称.");

		int ctype = corp.getCtype();
		int valid = 0;
		List<ConstItem> list = DicConstUtils.getConst("SYS2", "ctype", false);
		for(ConstItem item: list) {
			int id = StrTool.strToInt(item.getId().toString(), 0);
			if ((id & ctype)>0) valid += id;
		}
		if (valid<=SysCorpBean.SUPPER_CORP)
			throw new AppException(SysErr.SYS_PARAM_ERR, "不支持登记此类型企业.");
		
		corp.setCtype(valid);
		return corp;
	}
}
